package lib.popup

import android.content.Context
import androidx.annotation.LayoutRes
import androidx.viewbinding.ViewBinding

/**
 *  author: luoXiong
 *  e-mail: 382060748@qq.com
 *  date: 2021/9/2 17:50
 *  version: 1.0
 *  desc:
 */
//        new XPopup.Builder(DemoActivity.this)
//                .autoDismiss(false)
//                .asBottomList("haha", new String[]{"点我显示弹窗",
//                "点我显示弹窗", "点我显示弹窗", "点我显示弹窗"}, new OnSelectListener() {
//                    @Override
//                    public void onSelect(int position, String text) {
//                    }
//                }).show();
object Popup {
    fun t1(context: Context) {
        val asConfirm = Popup.asConfirm(context)

        //asConfirm.maxWidth(123).confirmBtn()

    }

    fun asConfirm(context: Context): ConfirmBuilder {
        return ConfirmBuilder(context)
    }

    fun asMsg(context: Context): MessageBuilder {
        return MessageBuilder(context)
    }
}

abstract class BasePopupBuilder<builder : BasePopupBuilder<builder>>(val context: Context) {
    protected val popupInfo = PopupCommonInfo()

    /**
     * 设置高度，如果重写了弹窗的getPopupHeight，则以重写的为准
     * 并且受最大高度限制
     * @param height
     * @return
     */
    fun popupHeight(height: Int): builder {
        this.popupInfo.popupHeight = height
        return this as builder
    }

    /**
     * 设置宽度，如果重写了弹窗的getPopupWidth，则以重写的为准
     * 并且受最大宽度限制
     * @param width
     * @return
     */
    protected fun popupWidth(width: Int): builder {
        this.popupInfo.popupWidth = width
        return this as builder
    }

    /**
     * 设置最大宽度，如果重写了弹窗的getMaxWidth，则以重写的为准
     * protected
     * @param maxWidth
     * @return
     */
    open fun maxWidth(maxWidth: Int): builder {
        this.popupInfo.maxWidth = maxWidth
        return this as builder
    }

    fun setCustomLayoutId(@LayoutRes layoutId: Int): builder {
        return this as builder
    }
}

/**
 * 确认对话框
 */
class ConfirmBuilder(context: Context) : BasePopupBuilder<ConfirmBuilder>(context) {
    private var mPw: _ConfirmPopupView = _ConfirmPopupView(context)

    fun show() {
        mPw.show(popupInfo)
    }

    fun title(title: String): ConfirmBuilder {
        mPw.mTitle = title
        return this
    }

    fun content(content: String): ConfirmBuilder {
        mPw.mContent = content
        return this
    }

    fun rightBtn(text: String, colorInt: Int? = null, clickListener: () -> Unit): ConfirmBuilder {
        mPw.mRightBtnText = text
        mPw.mRightBtnColorInt = colorInt
        mPw.mRightBtnClickListener = clickListener
        return this
    }

    fun leftBtn(text: String, colorInt: Int? = null, clickListener: () -> Unit): ConfirmBuilder {
        mPw.mLeftBtnText = text
        mPw.mLeftBtnColorInt = colorInt
        mPw.mLeftBtnClickListener = clickListener
        return this
    }
}

/**
 * 消息对话框
 */
class MessageBuilder(context: Context) : BasePopupBuilder<MessageBuilder>(context) {
    private var mPw = _MessagePopupView(context)

    fun show() {
        mPw.show(popupInfo)
    }

    fun title(title: String): MessageBuilder {
        mPw.mTitle = title
        return this
    }

    fun content(content: String): MessageBuilder {
        mPw.mContent = content
        return this
    }

    fun leftBtn(text: String, colorInt: Int? = null, clickListener: () -> Unit): MessageBuilder {
        mPw.mBtnText = text
        mPw.mBtnColorInt = colorInt
        mPw.mBtnClickListener = clickListener
        return this
    }
}


class CustomLayoutBuilder<V : ViewBinding>(context: Context) : BasePopupBuilder<CustomLayoutBuilder<V>>(context) {

    // private var mPw = _CustomPopupView(context)
    fun show() {

    }

    fun <V : ViewBinding> setLayoutId(@LayoutRes layoutId: Int, createdCb: (V) -> Unit): CustomLayoutBuilder<V> {
//        this.mCustomLayoutId = layoutId
//        this.mOnLayoutCreated = createdCb as (Any) -> Unit
        return this as CustomLayoutBuilder<V>
    }
}
